import pick from 'lodash/pick';
const filterFn = (condition, data) => condition && condition.length ? condition.some(key => Object.values(data).includes(key)) : true;

const filterData = {
  count: (data, condition, date) => {
    let maxDate;
    if (date) {
      maxDate = date === 'max' ? window.maxDate : date;
    }
    return data.reduce((acc, cur) => {
      const filterRs = (!maxDate || cur.date === maxDate) && filterFn(condition, cur);
      return filterRs ? acc + cur.count : acc;
    }, 0)
  },
  countByList: (data, field, department, date) => {
    date = date || window.maxDate;
    return data.filter(item => item.date === date && department.includes(item[field]))
  },
  countAllList: (data, department = 'grid') => {
    const dates = Array.from(new Set(data.map(item => item.date))).sort((a, b) => a - b);
    const matchedData = dates.map(date => data.filter(row => row.date === date));
    return {
      date: dates,
      data: matchedData.map((row, index) => (row.map(item => ({ name: item[department], value: item.count }))))
    }
  },
  kpi: (data, department, date, isReturnArray) => {
    date = date || window.maxDate;
    if (!department.length) department = ['全市'];
    const matchedItem = data.find(item => item['日期'] === date);
    if (!matchedItem) {
      return NaN
    }
    if (isReturnArray) {
      return department.reduce((acc, cur) => [...acc, { name: cur, value: matchedItem[cur] }], [])
    } else {
      return department.reduce((acc, cur) => acc + matchedItem[cur], 0) / department.length;
    }
  },
  kpiAllList: (data) => {
    data.sort((a, b) => a['日期'] - b['日期']);
    return {
      date: data.map(item => item['日期']),
      data: data.map(item => (Object.keys(item).map(field => ({ name: field, value: item[field] }))))
    }
  },
  /**
   * 
   * @param {Array<{date:number,region:string,grid:string,count:number}>} data 
   * @param {Array<string>} condition 
   * @param {boolean} toArray 
   * @returns {Array<{date:number,count:number}>}
   */
  countDate: (data, condition, toArray = false) => {
    const dateMap = data.reduce((dateMap, data) => {
      const date = data.date;
      const filterRs = filterFn(condition, data);
      if (filterRs) {
        if (!dateMap.has(date)) {
          dateMap.set(date, data.count)
        } else {
          dateMap.set(date, dateMap.get(date) + data.count)
        }
      }
      return dateMap;
    }, new Map())

    const rs = [...dateMap.keys()]
      .sort((a, b) => a - b)
      .map(date => {
        return toArray ? [date, dateMap.get(date)] : { date, count: dateMap.get(date) }
      })

    return rs;
  },
  /**
   * 
   * @param {'date'|'region'|'grid'} field 
   * @param {array} data 
   * @param {string[]} condition 
   * @param {boolean} toArray 
   * @returns {Array<{fieldValue: number|string,count:number}|Array<[fieldValue,count]>>}
   */
  countByField: (field, data, condition, toArray = false) => {
    const fieldMap = data.reduce((fieldMap, data) => {
      const filterRs = filterFn(condition, data);
      const fieldValue = data[field];
      if (filterRs) {
        if (!fieldMap.has(fieldValue)) {
          fieldMap.set(fieldValue, data.count)
        } else {
          fieldMap.set(fieldValue, fieldMap.get(fieldValue) + data.count)
        }
      }
      return fieldMap;
    }, new Map())

    const rs = [...fieldMap.keys()]
      .map(fieldValue => {
        return toArray ? [fieldValue, fieldMap.get(fieldValue)] : { [field]: fieldValue, count: fieldMap.get(fieldValue) }
      })

    return rs;
  },
  countNlByDate: (collection, fields) => {
    const map = new Map();
    collection.forEach(row => {
      let match = true;
      if (match) {
        if (!map.has(row.date)) {
          map.set(row.date, pick(row, fields))
        } else {
          let data = map.get(row.date);
          fields.forEach(field => data[field] += row[field])
          map.set(row.date, data)
        }
      }
    })

    const data = [...map.keys()].map(date => {
      return { date, ...map.get(date) }
    })
    return data
  },
  pickData: (data, condition) => {
    return data.map(row => {
      return pick(row, ['日期', ...condition])
    })
  },
  getTableData: (fieldName, fieldWidth, source) => {
    let tableCols = [], tableRows = [];
    fieldName.forEach((item, index) => {
      tableCols.push({
        key: `col-${index}`,
        dataKey: `col-${index}`,
        title: item[Object.keys(item)[0]],
        align: "center",
        width: fieldWidth[index]
      });
    });
    source.forEach((item, index) => {
      const row = { id: `row-${index}` };
      fieldName.forEach((field, fieldIndex) => {
        const key = Object.keys(field)[0];
        row[`col-${fieldIndex}`] = item[key]
      });
      tableRows.push(row);
    });
    return { tableCols, tableRows };
  }

}
window.filterData = filterData;
export default filterData;